package view;




import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.opengl.GLUtils;

class GLText {
   private  FloatBuffer mVertexBuffer;
   
   
   private  FloatBuffer mTextureBuffer;

   
   public GLText() {
      
 
      
   }
   

   public void draw(GL10 gl) {  // 这里应该保护下渲染状态....
      gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
      
      
      gl.glEnable(GL10.GL_TEXTURE_2D); // workaround bug 3623
      gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer);
      
      
      // FRONT
      gl.glColor4f(1, 1, 1, 1);
      gl.glNormal3f(0, 0, 1);
      gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

   }
   
   
   void loadStr(GL10 gl, String str){
	   
	   ///*
 	   Bitmap bmp  = generate_bitmap(str);
 	      GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
 	      gl.glTexParameterx(GL10.GL_TEXTURE_2D,
 	            GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
 	      gl.glTexParameterx(GL10.GL_TEXTURE_2D,
 	            GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
 	      bmp.recycle();
 	     // */ 
	 ///*  
	     float one = 1.0f;
	     float delta = (float)bmp.getWidth()/bmp.getHeight();
	     float  xHalf = (one )/2;
	     float yHalf = (one / delta)/2;
	      float half = one / 2;
	   //*/    
	/*      
		     float one = 1.0f;
		     float  xHalf = one / 2;
		     float yHalf = (one )/2;
		      float half = one / 2;
	  */    
	      
	      
	   
	      float vertices[] = { 
	            // FRONT
	            -xHalf, -yHalf, half, xHalf, -yHalf, half,
	            -xHalf, yHalf, half, xHalf, yHalf, half,};

	      
	      
	      float texCoords[] = {
	            // FRONT
	            0, one, one, one, 0, 0, one, 0,
	           };
	      

	      
	      // Buffers to be passed to gl*Pointer() functions must be
	      // direct, i.e., they must be placed on the native heap
	      // where the garbage collector cannot move them.
	      //
	      // Buffers with multi-byte data types (e.g., short, int,
	      // float) must have their byte order set to native order
	      ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
	      vbb.order(ByteOrder.nativeOrder());
	      mVertexBuffer = vbb.asFloatBuffer();
	      mVertexBuffer.put(vertices);
	      mVertexBuffer.position(0);
	      

	      
	      // ...
	      ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4);
	      tbb.order(ByteOrder.nativeOrder());
	      mTextureBuffer = tbb.asFloatBuffer();
	      mTextureBuffer.put(texCoords);
	      mTextureBuffer.position(0);
   }
   
   
    void loadTexture(GL10 gl, Context context, int resource) {
	   
	// /*  
      Bitmap bmp = BitmapFactory.decodeResource(
            context.getResources(), resource);
      GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
      gl.glTexParameterx(GL10.GL_TEXTURE_2D,
            GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
      gl.glTexParameterx(GL10.GL_TEXTURE_2D,
            GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
      bmp.recycle();
     // */
	   
 	 


   }
   
   
   
    Bitmap generate_bitmap(String s){
	   
	   Paint paint = new Paint();
	   paint.setTextSize(20);
	//   paint.setARGB(255, r, g, b)
	  paint.setFlags(Paint.ANTI_ALIAS_FLAG); // 打开抗锯齿
/*
	  Bitmap bmp = Bitmap.createBitmap(128, 128, Bitmap.Config.ARGB_8888);
	  Canvas canvas = new Canvas(bmp);
	  
	  canvas.drawText(s, 0, 10, paint);
	*/
	  

	   
	   float bitmap_width =0;
	  float bitmap_height =0;
	 
	  Paint paintStroke = new Paint();
	  paintStroke.setColor(Color.RED);
	  paintStroke.setStyle(Style.STROKE);
	  paintStroke.setStrokeWidth(2);
	  
	  bitmap_width = paint.measureText(s);
	  Rect bounds = new Rect();
	  paint.getTextBounds(s, 0, s.length(), bounds);
	  bitmap_height = bounds.height();
	  
	  Bitmap bmp = Bitmap.createBitmap((int)bitmap_width, (int)bitmap_height, Bitmap.Config.ARGB_8888);
	  Canvas canvas = new Canvas(bmp);
	  RectF r = new RectF(0,0,bitmap_width,bitmap_height);
//	  canvas.drawRect(r, paintStroke);
	  
	    
	  
	  paint.setColor(Color.BLUE);
	  canvas.drawText(s, 0, bitmap_height-2, paint); //使脚底有些空隙
	  
	  
	  return bmp;
   }

   
//   public int width ;
//   public int height ;
   
   
}

